package com.cb.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cb.eduservice.entity.EduCourse;
import com.cb.eduservice.entity.EduTeacher;
import com.cb.eduservice.service.EduCourseService;
import com.cb.eduservice.service.EduTeacherService;
import com.cb.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 前台首页管理
 *
 * @author cb
 * @version 1.0
 * @date 2020/6/26 18:38
 */
@Api(description = "前台首页信息管理")

@RestController
@RequestMapping("/eduservice/indexfront")
public class IndexFrontController {

    private final EduCourseService courseService;
    private final EduTeacherService teacherService;

    @Autowired
    public IndexFrontController(EduCourseService courseService, EduTeacherService teacherService) {
        this.courseService = courseService;
        this.teacherService = teacherService;
    }

    @ApiOperation("获取前八条热门课程, 前4条名师")
    @GetMapping("index")
    public R index() {
        // 查询前八条热门课程
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<>();
        courseWrapper.orderByDesc("buy_count");
        courseWrapper.last("limit 8");
        List<EduCourse> courseList = courseService.list(courseWrapper);

        // 查询前四条名师
        QueryWrapper<EduTeacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("sort");
        teacherWrapper.last("limit 4");
        List<EduTeacher> teacherList = teacherService.list(teacherWrapper);

        return R.ok()
                .data("course", courseList)
                .data("teacher", teacherList);
    }

}
